const Router = require('koa-router')
const Record = require('../schema/Record')
const moment = require('moment')
const axios = require('axios')
const config = require('../config')
const tools = require('../utils/tools')

const recordRouter = new Router({
  prefix: '/recordApi/records'
})
// 新建病历 或 保存病历
recordRouter.put('/save', async ctx => {
  let body = ctx.request.body
  body.record.PE.values = JSON.stringify(body.record.PE.values)
  let record = await Record.findOne({
    'patientInfo.id': body.record.patientInfo.id,
    'templateId': body.templateId,
    'state': 'save'
  })
  // 不存在 则新建
  if (record == null) {
    let sub = tools.getSub(ctx.header.authorization);
    // 删除模板主键
    delete body.record._id
    body.record.templateId = body.templateId
    body.record.datetime = moment().format('YYYY-MM-DD h:mm:ss')
    body.record.doctor = sub.opCode
    body.record.doctorName = sub.opName
    body.record.groups = sub.groups
    body.record.dept = sub.departId
    body.record.deptName = sub.departName
    record = await new Record(body.record).save()
    // 关联结构化数据表
    let res = await axios.post(`${config.api}/patients/save/${record.patientInfo.id}`, {
      recordDocId: record._id
    })
  } else {
    delete body.record.__v
    record.set(body.record)
    await record.save()
  }
  ctx.body = {
    result: 'success',
    record: record
  }
})
// 确诊
recordRouter.patch('/confirm/:patientId/:recordId', async ctx => {
  let patientId = ctx.params.patientId
  let recordId = ctx.params.recordId
  let record = await Record.findOneAndUpdate({
    '_id': recordId
  }, { $set: { 'state': 'confirm' }})
  let _record = {
    computer: [],
    manually: []
  }
  record.diagnosis.forEach(e => {
    if (e._type == 'computer') {
      _record.computer.push(e)
    } else if (e._type == 'manually') {
      _record.manually.push(e)
    }
  })
  await axios.post(`${config.api}/patients/confirm/${patientId}`, _record)
  ctx.body = {
    result: 'success'
  }
})
// 作废
recordRouter.delete('/cancel/:patientId/:recordId', async ctx => {
  let patientId = ctx.params.patientId
  let recordId = ctx.params.recordId
  await Record.deleteOne({
    '_id': recordId
  })
  await axios.post(`${config.api}/patients/cancel/${patientId}`)
  ctx.body = {
    result: 'success'
  }
})

// 历史病历
recordRouter.get('/history/:patientId', async ctx => {
  let patientId = ctx.params.patientId
  let records = await Record.find({
    'patientInfo.patientId': patientId,
    'state': 'confirm'
  })
  ctx.body = records
})
// 当前正在进行的病历(唯一)
recordRouter.get('/now/:patientId/:templateId', async ctx => {
  let patientId = ctx.params.patientId
  let templateId = ctx.params.templateId
  let record = await Record.findOne({
    'patientInfo.id': patientId,
    'templateId': templateId,
    'state': 'save'
  })
  ctx.body = record
})

// 多id查询
recordRouter.post('/getByIds', async ctx => {
  let ids = ctx.request.body
  let records = await Record.find({"_id": ids})
  ctx.body = records
})


module.exports = recordRouter